package com.kgc.cgprovider.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kgc.basic.util.ResponseMsg;
import com.kgc.cgapi.pojo.*;
import com.kgc.cgprovider.dao.CGDao;
import com.kgc.cgprovider.dao.GYBJDao;
import com.kgc.cgprovider.service.CGServ;
import com.kgc.cgprovider.service.GYBJService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class GYBJServiceImpl implements GYBJService {

    private Logger logger = LoggerFactory.getLogger(GYBJServiceImpl.class);

    @Resource
    private GYBJDao gybjDao;
    @Resource
    private CGServ cgServ;


    @Override
    public ResponseMsg getPage(Map<String, Object> map) {
        ResponseMsg responseMsg = new ResponseMsg();
        int pageNow = 0;
        int pageSize = 0;
        if(map.get("pageNum") instanceof String){
            pageNow =Integer.parseInt((String) map.get("pageNum"));
            pageSize =Integer.parseInt((String) map.get("pageSize"));
        }else {
            pageNow = (Integer) map.get("pageNum");
            pageSize = (Integer) map.get("pageSize");
        }
        PageHelper.startPage(pageNow,pageSize);
        List<GYBJPojo> list = gybjDao.getGYBJPage(map);
        PageInfo<GYBJPojo> pageInfo = new PageInfo<>(list);
        responseMsg.setCode(200);
        responseMsg.setData(pageInfo);
        return responseMsg;
    }

    @Override
    public ResponseMsg addGYBJ(Map<String, Object> map) {
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setCode(500);
        ArrayList<Map<String, Object>> arr =(ArrayList<Map<String, Object>>) map.get("list");
        String code ="GYBJ"+"-"+ cgServ.getCode("cg_cgybj");
        map.put("code",code);
        Date date = new Date();
        map.put("create_date",date);
        int result = gybjDao.addGYBJ(map);
        logger.info("addGYBJ  + map {} result:{}",map,result);
        if (result <= 0){
            return responseMsg;
        }
        for (Map<String,Object> info : arr){
            info.put("major_code",code);
            info.put("create_date",date);
            info.put("create_id",map.get("create_id"));
            result = gybjDao.addGYBJInfo(info);
            logger.info("addGYBJInfo  + info {} result:{}",info,result);
            if (result <= 0){
                return responseMsg;
            }
        }
        responseMsg.setData(code);
        responseMsg.setCode(200);
        return responseMsg;
    }

    @Override
    public ResponseMsg getGYBJ(String code) {
        ResponseMsg responseMsg = new ResponseMsg();
        GYBJPojo gybjInfo = gybjDao.getGYBJ(code);
        logger.info("getGYBJ  + code:{} gybjPojo{}",code,gybjInfo);
        List<GYBJInfo> list = gybjDao.getGYBJInfo(code);
        gybjInfo.setList(list);
        responseMsg.setCode(200);
        responseMsg.setData(gybjInfo);
        return responseMsg;
    }

    @Override
    public ResponseMsg modiGYBJ(Map<String, Object> map) {
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setCode(500);
        String code = (String) map.get("code");
        Date date = new Date();
        ArrayList<Map<String, Object>> arr =(ArrayList<Map<String, Object>>) map.get("list");
        map.put("modi_date",date);
        int modiGYBJ = gybjDao.modiGYBJ(map);
        int del = gybjDao.delGYBJInfo(code);

        logger.info("modiGYBJ result  + map {} result:{} + isDelInfo{}",map,modiGYBJ,del);
        for (Map<String,Object> info : arr){
            info.put("major_code",code);
            info.put("modi_date",date);
            info.put("modi_id",map.get("create_id"));
            info.put("create_date",date);
            info.put("create_id",map.get("create_id"));

            int result = gybjDao.addGYBJInfo(info);
            logger.info("addGYBJInfo  + info {} result:{}",info,result);
            if (result <= 0){
                return responseMsg;
            }
        }
        responseMsg.setCode(200);
        return responseMsg;
    }

    @Override
    public ResponseMsg getGYBJByCGXJ(String xj_code) {
        ResponseMsg responseMsg = new ResponseMsg();
        List<GYBJPojo> list = gybjDao.getGYBJByCGXJ(xj_code);
        responseMsg.setData(list);
        responseMsg.setCode(200);
        return responseMsg;
    }

    @Override
    public ResponseMsg closeGYBJByCGXJ(String xj_code) {
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setCode(500);
        int result = gybjDao.closeGYBJByCGXJ(xj_code);
        if(result > 0 ){
            responseMsg.setCode(200);
        }
        return responseMsg;
    }
}
